package com.example.jpa.repository;

import com.example.jpa.pojo.Customer;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;


/**
 * jpa sql语句通过这个自动实现
 * JpaRepository<T, ID>,T泛型类,ID主键，普通增删改查，普通查询这一个就够了
 * JpaSpecificationExecutor,动态条件查询
 */

public interface Operate extends JpaRepository<Customer,Long>, JpaSpecificationExecutor<Customer> {


    //方法二、方法命名查询,jpa自带的方法名就可以查询了，基本不用
    //根据名字查询
    List<Customer> findByName(String name);
    //模糊查询,name跟addr都模糊
    List<Customer> queryByNameLikeAndAddressLike(String name, String address);


    //方法三：jpql查询,原生sql把nativeQuery==true
    @Query("from Customer where name like %?1%")
    List<Customer> findCustomerByName(String name);

    @Query("from Customer where address like %:addr%")
    List<Customer> findByAddressLike(@Param("addr") String address);

    //更新操作
    @Modifying
    @Query("update Customer set name = ?1 where address = ?2")
    void update(String name, String address);
}
